package com.itheima.mapper;

import com.github.pagehelper.Page;
import com.itheima.domain.Book;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

public interface BookMapper {

    @Select("select * from books where book_status != 3 order by book_uploadtime desc")
    @Results(id = "BookMap",value = {
            @Result(id = true,property = "id",column = "book_id"),
            @Result(property = "name",column = "book_name"),
            @Result(property = "isbn",column = "book_isbn"),
            @Result(property = "press",column = "book_press"),
            @Result(property = "author",column = "book_author"),
            @Result(property = "pagination",column = "book_pagination"),
            @Result(property = "uploadTime",column = "book_uploadtime"),
            @Result(property = "price",column = "book_price"),
            @Result(property = "status",column = "book_status"),
            @Result(property = "borrower",column = "book_borrower"),
            @Result(property = "borrowTime",column = "book_borrowtime"),
            @Result(property = "returnTime",column = "book_returntime")
    })
    Page<Book> selectBooks();

    @Select("select * from books where book_id = #{id}")
    @ResultMap("BookMap")
    Book findById(String id);


    Integer borrowBook(Book book);

    @Select("<script>\n" +
            "        select * from books\n" +
            "        <where>\n" +
            "            <if test=\"name!=null and name.trim()!= ''\">\n" +
            "                and book_name like concat ('%' ,#{name},'%')\n" +
            "            </if>\n" +
            "            <if test=\"author!=null and author.trim()!= ''\">\n" +
            "                and book_author like concat ('%' ,#{author},'%')\n" +
            "            </if>\n" +
            "            <if test=\"press!=null and press.trim()!= ''\">\n" +
            "                and book_press like concat ('%' ,#{press},'%')\n" +
            "            </if>\n" +
            "        </where>\n" +
            "        order by book_status\n" +
            "        </script>")
    @ResultMap("BookMap")
    Page<Book> searchBooks(Book book);

    //添加图书信息
    Integer addBook(Book book);

    //修改图书信息
    Integer editBook(Book book);

    //查询当前用户未归还的图书信息
    @Select("<script>\n" +
            "        select * from books\n" +
            "        where book_borrower = #{borrower}\n" +
            "        and book_status in (1,2)\n" +
            "        <if test=\"name != null and name.trim()!=''\">\n" +
            "            and book_name like concat ('%' ,#{name},'%')\n" +
            "        </if>\n" +
            "        <if test=\"author != null and author.trim()!=''\">\n" +
            "            and book_author like concat ('%' ,#{author},'%')\n" +
            "        </if>\n" +
            "        <if test=\"press != null and press.trim()!=''\">\n" +
            "            and book_press like concat ('%' ,#{press},'%')\n" +
            "        </if>\n" +
            "        </script>")
    @ResultMap("BookMap")
    Page<Book> selectMyBorrowed(Book book);

    //查询当前用户未归还和所有待确认归还的图书信息
    @Select("<script>\n" +
            "        select * from books\n" +
            "        where book_borrower = #{borrower}\n" +
            "        and book_status = 1\n" +
            "        <if test=\"name != null and name.trim()!=''\">\n" +
            "            and book_name like concat ('%' ,#{name},'%')\n" +
            "        </if>\n" +
            "        <if test=\"author != null and author.trim()!=''\">\n" +
            "            and book_author like concat ('%' ,#{author},'%')\n" +
            "        </if>\n" +
            "        <if test=\"press != null and press.trim()!=''\">\n" +
            "            and book_press like concat ('%' ,#{press},'%')\n" +
            "        </if>\n" +
            "\n" +
            "        or book_status = 2\n" +
            "        <if test=\"name != null and name.trim()!=''\">\n" +
            "            and book_name like concat ('%' ,#{name},'%')\n" +
            "        </if>\n" +
            "        <if test=\"author != null and author.trim()!=''\">\n" +
            "            and book_author like concat ('%' ,#{author},'%')\n" +
            "        </if>\n" +
            "        <if test=\"press != null and press.trim()!=''\">\n" +
            "            and book_press like concat ('%' ,#{press},'%')\n" +
            "        </if>\n" +
            "        </script>")
    @ResultMap("BookMap")
    Page<Book>  selectBorrowed(Book book);
}
